package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.DataDowConst;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysDataDow;
import com.bysj.sausys.service.ISysDataDowService;
import com.bysj.sausys.util.PropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 资源下载Controller
 *
 * @author WangZK
 */
@Api(description = "资源下载管理接口")
@Controller
@RequestMapping("/sys/datadow")
public class SysDataDowController {

    @Resource
    private ISysDataDowService sysDataDowService;

    /**
     * 添加下载资源
     *
     * @param sysDataDow
     * @param request
     * @return
     */
    @ApiOperation(value = "添加下载资源")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(@ApiParam(value = "资源实体类", required = true) @RequestBody SysDataDow sysDataDow,
                         HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDataDowService.save(sysDataDow, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 更新下载资源
     *
     * @param sysDataDow
     * @param request
     * @return
     */
    @ApiOperation(value = "更新下载资源")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData update(@ApiParam(value = "资源实体类", required = true) @RequestBody SysDataDow sysDataDow,
                           HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDataDowService.update(sysDataDow, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 分页获取列表
     *
     * @param keyword
     * @param pageNo   页码
     * @param pageSize 每页显示条数
     * @return
     */
    @ApiOperation(value = "分页获取资源下载列表接口")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                            @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize,
                            HttpServletRequest request) {
        String path = request.getSession().getServletContext()
                .getRealPath("/");
        PageResult result = sysDataDowService.getPageByKeyword(path, keyword, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据id获取下载资源
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取下载资源接口")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "下载资源id", required = true) @RequestParam Integer id) {
        SysDataDow sysDataDow = sysDataDowService.findById(id);
        return JsonData.success(sysDataDow);
    }

    /**
     * 修改下载资源的状态为启用
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "修改下载资源的状态为启用接口")
    @RequestMapping(value = "/setDataDowIsEnable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData setDataDowIsEnable(@ApiParam(value = "下载资源id", required = true) @RequestParam Integer id,
                                       HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDataDowService.setDataDowStatus(id, DataDowConst.ENABLE.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 修改下载资源的状态为禁用
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "修改下载资源的状态为禁用")
    @RequestMapping(value = "/setDataDowIsDisable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData setDataDowIsDisable(@ApiParam(value = "下载资源id", required = true) @RequestParam Integer id,
                                        HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDataDowService.setDataDowStatus(id, DataDowConst.DISABLE.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 删除下载资源
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除下载资源")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData delete(@ApiParam(value = "下载资源id", required = true) @RequestParam Integer id,
                           HttpServletRequest request) {
        String path = request.getSession().getServletContext()
                .getRealPath("/");
        sysDataDowService.delete(id, path);
        return JsonData.success();
    }

}
